package com.example.vue20220402.controller;

import com.example.vue20220402.config.MyEmail;
import com.example.vue20220402.pojo.BaseResult;
import com.example.vue20220402.pojo.Usersinfo;
import com.example.vue20220402.service.IUsersinfoService;
import com.example.vue20220402.util.ImageUtil;
import com.example.vue20220402.vo.Constant;
import com.example.vue20220402.vo.ResultData;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/user")
public class UserController {

    @RequestMapping("/test")
    public Map<String,Object> test(){
        Map<String,Object> map = new HashMap<>();
        map.put("message","给前端的信息");
        map.put("username","张三");
        return map;
    }
    @RequestMapping("/test1")
    public String test1(){
        return "aaa";
    }

    @Resource
    IUsersinfoService usersinfoService;
    @RequestMapping("/userlist")
    public Map<String,Object> userlist(){
        return usersinfoService.userlist();
    }

    @RequestMapping("/login")
    public Map<String,Object> login(Usersinfo usersinf){
        System.out.println(usersinf);
        return usersinfoService.login(usersinf);
    }
    @RequestMapping("/register")
    public Map<String,Object> register(Usersinfo usersinf){
        System.out.println(usersinf);
        return usersinfoService.register(usersinf);
    }
    @RequestMapping("/updateUser")
    public ResultData updateUser(Usersinfo usersinf){
        System.out.println(usersinf);
        return usersinfoService.updateUser(usersinf);
    }
    @RequestMapping("/deleteUser")
    public ResultData deleteUser(Usersinfo usersinf){
        System.out.println(usersinf);
        return usersinfoService.deleteUser(usersinf);
    }
    @RequestMapping("/exportUser")
    public void exportUser(HttpServletResponse response){
        usersinfoService.exportUser(response);
    }
    @RequestMapping("/fileUpload")
    public ResultData fileUpload(MultipartFile file ,Integer id){
        ResultData rd = new ResultData();
        System.out.println(id+"---");
        if(file.isEmpty()){
            rd.setCode(Constant.ERROR.getCode());
            rd.setMessage("图片不存在");
            return rd;
        }
        String filename = file.getOriginalFilename();
        if(!filename.contains(".png") && !filename.contains(".gif") &&!filename.contains(".jpg") &&!filename.contains(".jpeg")){
            rd.setCode(Constant.ERROR.getCode());
            rd.setMessage("图片格式不正确");
            return rd;
        }
        String toFilePath = "D:\\Users\\image";
        File setfile = new File(toFilePath+"/"+id+filename);
        String resoucePath = "http://localhost:8080/file";
        List<String> strings = null;
        try {
            if(!setfile.exists()){  //  图片没有  去写入
                file.transferTo(setfile); //  file  复制到  setfile
                String[] str = new String[]{setfile.getAbsolutePath()};
                strings = ImageUtil.generateThumbnail2Directory(toFilePath, str);// 生成压缩图片
                rd.setCode(Constant.OK.getCode());
                Usersinfo usersinfo = new Usersinfo();
                usersinfo.setId(id);
                //  把toFilePath  替换成  resoucePath
                System.out.println(strings.get(0));
                String imgsrc = strings.get(0).replace(toFilePath,resoucePath);
                System.out.println(imgsrc);
                usersinfo.setImgsrc(imgsrc);
                usersinfoService.updateUser(usersinfo);
                rd.setResult(imgsrc);  // 把替换后的地址  传给前台
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return rd;
    }


    @Resource
    MyEmail myEmail;
    @Resource
    RedisTemplate redisTemplate;

    @RequestMapping("/sendemail")
    public Map<String,Object> sendemail(String email){
        // 取出 这个验证码   如果存在的话  验证码 还没有过期
        //  不存在的话   重新发送验证码
        Object o = redisTemplate.opsForValue().get(email);
        Map<String,Object> map = new HashMap<>();
        if(o != null){
            map.put("info","验证码还未过期，不用重复发送");
            map.put("extime",redisTemplate.getExpire(email));
            return map;
        }
        System.out.println(email);
        //  发送验证码
        //  生成验证码
        Random random = new Random();
        String str="";
        for(int i=0;i<6;i++){
            str += random.nextInt(9);
        }
        //  验证码存到redis
        map = myEmail.sendEmail(email, "验证码是：" + str, "验证码");
        redisTemplate.opsForValue().set(email,str,5, TimeUnit.MINUTES);
        map.put("extime",redisTemplate.getExpire(email));
        return map;
    }

    @RequestMapping("/emaillogin")
    public Map<String,Object> emaillogin(Usersinfo usersinfo){
        System.out.println(usersinfo);
        Map<String,Object> map = new HashMap<>();
        //  从redis里面 取出 验证码
        String code = redisTemplate.opsForValue().get(usersinfo.getUsername())+"";
        System.out.println(code);
        if("".equals(code)){
            map.put("message","验证码失效或者请重新发送验证码");
            return map;
        }
        if(code.equals(usersinfo.getPwd())){
            map.put("message","登录成功");
        }else{
            map.put("message","登录失败");
        }
        return map;
    }

    @RequestMapping("/list")
    public ResultData list(Usersinfo usersinfo){
        System.out.println(usersinfo.getPage());
        return usersinfoService.list(usersinfo);
    }


}
